package com.xuwei.chatroom.listener;

import com.xuwei.chatroom.entity.User;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author xuwei
 * @Date 2020/8/9 23:12
 * @Description
 */
@Slf4j
@WebListener
public class ServiceListener implements ServletContextListener {
    // ServletContext对象创建 下面这个方法就会执行
    // ServletContextEvent事件对象. 监听器对象--->ServletContext对象.(事件源)
    public void contextInitialized(ServletContextEvent event) {
        log.info("容器初始化******************************************");
        Map<User, HttpSession> userMap = new HashMap<>();
        event.getServletContext().setAttribute("userMap", userMap);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("stop*******************************************");
    }
}
